<template>
  <view class="module-list">
    <view
      v-for="(item, index) in modules"
      :key="index"
      class="module-item"
      @click="handleModuleClick(item)"
    >
      <!-- <image :src="item.icon" class="module-icon"></image> -->
      <text :class="['iconfont', 'module-icon', item.icon]"></text>
      <text class="module-title">{{ item.title }}</text>
    </view>
  </view>
</template>

<script>
export default {
  name: "ModuleList",
  props: {
    modules: {
      type: Array,
      required: true,
      default: () => [
        { icon: "icon-x-suninggongyi", title: "宗亲荟", url: "/pages/work/clan/clanList" },
        { icon: "icon-header-19", title: "家族祠堂", url: "/pages/work/family/list" },
        { icon: "icon-zhengcefagui", title: "会员管理", url: "/pages/mine/clubmember/index" },
        { icon: "icon-header-19", title: "会员审核", url: "/pages/mine/process/index" },
        { icon: "icon-shizhanpeixun", title: "三修基金", url: "/pages/work/fund/index" },
        { icon: "icon-yuangongfengcai", title: "捐赠榜", url: "/pages/work/donate/index" },
        { icon: "icon-zhengcefagui", title: "家族活动", url: "" },
        { icon: "icon-xinwendongtai", title: "家族文化", url: "/pages/work/familyHistory/index" },
        { icon: "icon-chunxueicon-", title: "文史研究" },
        // { icon: 'icon-jingxuan', title: '博阳精选' },
        { icon: "icon-qiyexinxi ", title: "商企联谊", url: "/pages/work/clan/busalliance/buslist" },
        { icon: "icon-xinwendongtai", title: "组织架构" },
        { icon: "icon-tongzhitonggao", title: "通知通告" },
        // { icon: 'icon-header-19', title: '赋能培训' },
        // { icon: 'icon-tongzhitonggao', title: '李氏风采' }
      ],
    },
    joinStatus: {
      type: Number,
      default: 1,
    },
    familyId: {
      type: Number,
      default: null,
    },
  },
  methods: {
    getUserId() {
      return uni.getStorageSync("user-info").userFamilyId;
    },
    // 获取areaCodeScope的方法
    getAreaCodeScope() {
      try {
        const userInfo = uni.getStorageSync("user-info");
        if (userInfo && userInfo.areaCodeScope) {
          // 判断用户是否为族长
          return userInfo.areaCodeScope === 6 || userInfo.areaCodeScope === 2;
        }
        return null;
      } catch (error) {
        console.error("获取用户信息失败", error);
        return null;
      }
    },
    handleModuleClick(module) {
      const permissionArr = ["会员管理", "会员审核"];
      const commonArr = ["宗亲荟", "家族祠堂", "三修基金", "捐赠榜", "家族文化", "商企联谊"];
      const launchUrlArr = ["家族活动"];
      // 如果角色是族长，且已经加入家谱什么模块都能访问
      // 如果是族长，且加入了家谱
      console.log(this.getAreaCodeScope(), this.joinStatus);
      if (this.getAreaCodeScope() && this.joinStatus == 1) {
        if (permissionArr.indexOf(module.title) > -1 || commonArr.indexOf(module.title) > -1) {
          if (module.title == "宗亲荟" || module.title == "家族祠堂") {
            uni.navigateTo({
              url: module.url + `?type=once&familyId=${this.familyId}&mySelf=true`,
            });
          } else {
            uni.navigateTo({
              url: module.url + `?type=once&familyId=${this.familyId}`,
            });
          }
          return;
        } else if (launchUrlArr.indexOf(module.title) > -1) {
          uni.reLaunch({
            url: module.url,
          });
          return;
        }
      } else {
        // 如果不是族长，且未加入家谱，只能访问commonArr的模块
        if (commonArr.indexOf(module.title) > -1) {
          uni.navigateTo({
            url: module.url + `?type=once&familyId=${this.familyId}`,
          });
          return;
        } else if (launchUrlArr.indexOf(module.title) > -1) {
          uni.reLaunch({
            url: module.url,
          });
          return;
        } else if (permissionArr.indexOf(module.title) > -1) {
          uni.showToast({
            title: "您没有权限执行此操作",
            icon: "none",
          });
          return;
        }
      }
      // 如果是族长（areaCodeScope为6）则提示无权限
      // if (!this.getAreaCodeScope() || this.joinStatus != 1) {
      //   const permissionArr = ['会员管理', '家谱成员', '会员审核'];
      //   if (this.joinStatus == 1) {
      //     const commonArr = ['宗亲荟', '家族祠堂', '三修基金', '捐赠榜', '家族活动', '家族文化'];
      //     if (permissionArr.indexOf(module.title) != -1) {
      //       uni.showToast({
      //         title: '您没有权限执行此操作',
      //         icon: 'none'
      //       });
      //       return;
      //     } else if (commonArr.indexOf(module.title) > -1) {
      //       console.log(module.url, module.title)
      //       // uni.reLaunch({
      //       //   url: module.url + '?familyId=' + this.familyId
      //       // });
      //       return;
      //     } else {
      //       uni.showToast({
      //         title: `功能待开发：${module.title}`,
      //         icon: 'none'
      //       });
      //       return;
      //     }
      //   } else {

      //   }

      // } else {
      //   if (module.url == '/pages/square/index') {
      //     uni.reLaunch({
      //       url: module.url
      //     });
      //     return;
      //   } else if (module.url == '/pages/work/family/list') {
      //     uni.navigateTo({
      //       url: '/pages/work/family/list?familyId=' + this.getUserId()
      //     });
      //     return;
      //   } else if (module.url == undefined) {
      //     uni.showToast({
      //       title: `功能待开发：${module.title}`,
      //       icon: 'none'
      //     });
      //     return;
      //   } else {
      //     if (module.url) {
      //       uni.navigateTo({
      //         url: module.url
      //       });
      //     }
      //   }
      //   return;
      // }
    },
  },
};
</script>

<style scoped>
.module-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
}

.module-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 31%;
  cursor: pointer;
  padding: 10px 0;
  background: #f5f5f5;
}

.module-icon {
  height: 80rpx;
  border-radius: 12rpx;
  object-fit: cover;
  margin-bottom: 10rpx;
  font-size: 50rpx;
  color: #00147b;
}

.module-title {
  font-size: 26rpx;
  color: #333;
  text-align: center;
}
</style>
